﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Qplus.Commands;
using System.Diagnostics;
using Qplus.beans;
using Qplus.enums;

namespace Qplus.UI
{
    public partial class PopupUserInfo : UserControl
    {
        bool isFriend;
        public PopupUserInfo()
        {
            InitializeComponent();
            try
            {
                name = (string)QUser.Instance.personalInfo["name"];
                sex = (int)QUser.Instance.personalInfo["sex"];
                mail = (string)QUser.Instance.personalInfo["mail"];
                birthday = (string)QUser.Instance.personalInfo["birthday"];
                canStrangerChat = (int)QUser.Instance.personalInfo["canStrangerChat"];
                level = (string)QUser.Instance.personalInfo["level"];
                headType = (int)QUser.Instance.personalInfo["headType"];
                headValue = (string)QUser.Instance.personalInfo["headValue"];
                MightList = null;//QUser.Instance.personalInfo["MightList;
                Current_max_friend_count = (string)QUser.Instance.personalInfo["Current_max_friend_count"];
                Max_friend_count_upper = (string)QUser.Instance.personalInfo["Max_friend_count_upper"];
                Task_score = (string)QUser.Instance.personalInfo["Task_score"];
                appList = null;//QUser.Instance.personalInfo["appList"];
            }
            catch (Exception ee)
            {
                Debug.WriteLine(ee.Message);
            }
        }

        #region
        public string name ;
        public int sex ;
        public string mail ;
        public string birthday ;
        public int canStrangerChat ;
        public string level ;
        public int headType ;
        public string headValue ;
        public MightList[] MightList ;
        public string Current_max_friend_count ;
        public string Max_friend_count_upper ;
        public string Task_score ;
        public appList[] appList ;
        //public string name { get; set; }
        //public int sex { get; set; }
        //public string mail { get; set; }
        //public string birthday { get; set; }
        //public int canStrangerChat { get; set; }
        //public string level { get; set; }
        //public int headType { get; set; }
        //public string headValue { get; set; }
        //public MightList[] MightList { get; set; }
        //public string Current_max_friend_count { get; set; }
        //public string Max_friend_count_upper { get; set; }
        //public string Task_score { get; set; }
        //public appList[] appList { get; set; }
        #endregion
        public UserInfo userinfo { get; set; }
        public long userID { get; set; }
        public string userName
        {
            set { nameTxt.Text = value; }
            get { return nameTxt.Text; }
        }
        public bool IsFriend
        {
            set
            {
                isFriend = value;
                if (isFriend)
                    starChatBtnEx.Visibility = System.Windows.Visibility.Collapsed;
            }
            get { return isFriend; }
        }
        public string userAge
        {
            set { ageTxt.Text = value; }
        }
        public E_SEX userSex
        {
            set
            {
                switch (value)
                {
                    case E_SEX.SEX_MAN:
                        sexTxt.Text = "男";
                        break;
                    case E_SEX.SEX_WOMEN:
                        sexTxt.Text = "女";
                        break;
                    case E_SEX.SEX_NOTSET:
                        sexTxt.Text = "";
                        break;
                    default:
                        sexTxt.Text = "";
                        break;
                }
            }
        }

        private void closeBtnEx_Click(object sender, RoutedEventArgs e)
        {
            this.CloseMeAsPopup();
        }

        private void starChatBtnEx_Click(object sender, RoutedEventArgs e)
        {
            this.CloseMeAsPopup();
            AddFriendCommand command = new AddFriendCommand(
                QUser.Instance.userID.ToString(),
                System.Text.Encoding.UTF8.GetString(QUser.Instance.key, 0, QUser.Instance.key.Length),
                QUser.Instance, QUser.Instance.getInfo());
            command.AddFriendCommandReceivedFromHttpServer += new EventHandler<Infrastructure.MessageReceivedFromServer>(command_AddFriendCommandReceivedFromHttpServer);
            command.set(0, this.userID);
            command.execute(QUser.Instance.url);
            //command.execute("http://192.168.1.88:8088/AppPortlet/");
        }

        void command_AddFriendCommandReceivedFromHttpServer(object sender, Infrastructure.MessageReceivedFromServer e)
        {
            //throw new NotImplementedException();
            Dispatcher.BeginInvoke(() =>
            {
                (App.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/UI/ChatFriend.xaml?nickName=" + userName, UriKind.Relative));
            });
        }

        private void reportBtnEx_Click(object sender, RoutedEventArgs e)
        {
            this.CloseMeAsPopup();
            PopupContainer pc = new PopupContainer((Application.Current as App).RootFrame.Content as PhoneApplicationPage);// ((Application.Current.RootVisual as PhoneApplicationFrame).GetVisualDescendants().OfType<PhoneApplicationPage>().FirstOrDefault());
            ReportControl popupUserInfo = new ReportControl() { userName = this.nameTxt.Text};
            pc.Show(popupUserInfo);
        }
    }
}
